package models

import "git.oschina.net/54json/gomysql"
import "fmt"

func GetArticlesList() (map[int]map[string]string, error) {
	m, err := gomysql.SetDB("default")
	rows := m.SetTable("article").OrderBy("id desc").FindAll()
	return rows, err
}

func GetArticleDetail(id string) (map[string]string, error) {
	m, err := gomysql.SetDB("default")
	row := m.SetTable("article").Where("id='" + id + "'").FindRow()
	return row, err
}
func DeleteArticle(id string) int {
	m, _ := gomysql.SetDB("default")
	row, _ := m.SetTable("article").Delete("id='" + id + "'")
	return row
}
func Save(article map[string]interface{}) int {
	fmt.Println(article)
	m, _ := gomysql.SetDB("default")
	if article["id"] != "" {
		fmt.Println(article)
		id := article["id"].(string)
		row, _ := m.SetTable("article").Where("id = '" + id + "'").Update(article)
		return row
	} else {
		delete(article, "id")
		row, _ := m.SetTable("article").Insert(article)
		return row
	}
	//row, _ := m.SetTable("article").Delete("id='" + id + "'")

}
